﻿(function() {
    function d(e, f) {
        return {
            title: "Insert Code Block",
            resizable: CKEDITOR.DIALOG_RESIZE_BOTH,
            minWidth: 310,
            minHeight: CKEDITOR.env.ie ? 350 : 320,
            onShow: function() {
                var txtarea = e.getSelection().getSelectedElement();
                if(txtarea)
                {
                    this.getContentElement('info', 'code').setValue(txtarea.getValue());
                    var selValParts = txtarea.getAttribute("class").split(":");
		            selVal = selValParts[0];
                    this.getContentElement('info', 'codeType').setValue(selVal);
		            for(var i=1; i < selValParts.length; i++)
			            if(selValParts[i] == 'nocontrols')
				           this.getContentElement('info', 'showcontrols').setValue(false);
			            else if(selValParts[i] == 'nogutter')
				           this.getContentElement('info', 'showgutter').setValue(false);
                }
            },
            onOk: function() {
                //debugger;
                var code = this.getContentElement('info', 'code').getValue();
                code = code.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
                var showGutter = this.getContentElement('info', 'showgutter').getValue();
                var showCtls = this.getContentElement('info', 'showcontrols').getValue();
                var codeType = this.getContentElement('info', 'codeType').getValue();
                if(!showCtls)
                    codeType += ":nocontrols";
                if(!showGutter)
                    codeType += ":nogutter";
                this._.editor.insertHtml("<pre><textarea class='" + codeType + "' rows='6' style='width: 70%' name='code'>" + code + "</textarea></pre>");
                return true;
            },
            contents: [{
                id: 'info',
                label: "label2",
                elements: [
                    {
                        type: 'vbox',
                        children: [
                            {
                                type: 'hbox',
                                styles: ['vertical-align:bottom'],
                                padding: 0,
                                children: [
                                    {
                                        type: 'select',
                                        id: 'codeType',
                                        'default': 'xml',
                                        label: "Code Type:",
                                        items: [['XML', 'xml'], ['C#', 'c#'], ['HTML', 'html']]
                                    }, {
                                        type: 'checkbox',
                                        id: 'showgutter',
                                        'default': true,
                                        label: "Show Line #"
                                    }, {
                                        type: 'checkbox',
                                        id: 'showcontrols',
                                        'default': true,
                                        label: "Show Toolbar"
                                    }
                                ]
                            }, {
                                type: 'textarea',
                                id: 'code',
                                rows: 15,
                                'default': '',
                                label: "Code:"
                            }
                        ]
                    }
                ]
}]
            };
        };

        CKEDITOR.dialog.add('syrinxCode', function(e) { return d(e, 'syrinxCode'); });
    })();